<template>
	<view class="login-page">
		<view class="login-title">用户登录</view>
		<view class="login-form">
			<view class="form-item">
				<view class="label">用户名</view>
				<input type="text" v-model="formData.username" placeholder="请输入用户名" @input="handleInput('username')"
					class="input-control" />
				<view class="error-tip" v-if="errors.username">{{ errors.username }}</view>
			</view>
			<view class="form-item">
				<view class="label">密码</view>
				<input type="password" v-model="formData.password" placeholder="请输入密码" @input="handleInput('password')"
					class="input-control" />
				<view class="error-tip" v-if="errors.password">{{ errors.password }}</view>
			</view>
			<button class="login-btn" @click="submitLogin" :disabled="isSubmitting">
				{{ isSubmitting ? '登录中...' : '登录' }}
			</button>
			<view class="other-actions">
				<text class="link" @click="goRegister">注册账号</text>
				<text class="link" @click="forgotPassword">忘记密码</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					username: '123',
					password: '123456'
				},
				errors: {
					username: '',
					password: ''
				},
				isSubmitting: false
			}
		},
		methods: {
			handleInput(field) {
				this.errors[field] = '';
			},
			validateForm() {
				let isValid = true;
				const {
					username,
					password
				} = this.formData;
				if (!username.trim()) {
					this.errors.username = '请输入用户名';
					isValid = false;
				} else if (username.trim().length < 3) {
					this.errors.username = '用户名至少3个字符';
					isValid = false;
				}
				if (!password) {
					this.errors.password = '请输入密码';
					isValid = false;
				} else if (password.length < 6) {
					this.errors.password = '密码至少6个字符';
					isValid = false;
				}

				return isValid;
			},
			submitLogin() {
				if (!this.validateForm()) {
					return;
				}
				this.isSubmitting = true;
				setTimeout(() => {
					const loginSuccess = true;
					if (loginSuccess) {
						uni.setStorageSync('userInfo', {
							username: this.formData.username,
							token: 'mock-token-' + Date.now(),
							loginTime: new Date().toLocaleString()
						});
						uni.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 1500
						});
						setTimeout(() => {
							uni.switchTab({
								url:'/pages/tabBar/tabcomPage/tabcomPage'
							})
						}, 1500);
					} else {
						uni.showToast({
							title: '用户名或密码错误',
							icon: 'none',
							duration: 2000
						});
						this.isSubmitting = false;
					}
				}, 1500);
			},
			jumpToHome() {
				const isTabBarPage = true;
				if (isTabBarPage) {
					uni.switchTab({
						url: '/pages/tabcomPage/tabcomPage',
						fail: (err) => {
							console.error('跳转失败:', err);
							uni.showToast({
								title: '跳转失败，请重试',
								icon: 'none'
							});
							this.isSubmitting = false;
						}
					});
				} else {
					uni.navigateTo({
						url: '/pages/tabcomPage/tabcomPage',
						fail: (err) => {
							console.error('跳转失败:', err);
							uni.showToast({
								title: '跳转失败，请重试',
								icon: 'none'
							});
							this.isSubmitting = false;
						}
					});
				}
			},
			goRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			forgotPassword() {
				uni.navigateTo({
					url: '/pages/forgot-password/forgot-password'
				});
			}
		}
	}
</script>

<style scoped>
	.login-page {
		display: flex;
		flex-direction: column;
		padding: 60rpx 30rpx;
		background-color: #f8f8f8;
		min-height: 100vh;
	}

	.login-title {
		font-size: 48rpx;
		font-weight: bold;
		color: #333;
		text-align: center;
		margin: 80rpx 0 100rpx;
	}

	.login-form {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 50rpx 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}

	.form-item {
		margin-bottom: 40rpx;
	}

	.label {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 15rpx;
		display: block;
	}

	.input-control {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 20rpx;
		border: 2rpx solid #eee;
		border-radius: 10rpx;
		font-size: 28rpx;
	}

	.input-control:focus {
		border-color: #007aff;
		outline: none;
	}

	.error-tip {
		font-size: 24rpx;
		color: #ff4d4f;
		margin-top: 10rpx;
		min-height: 30rpx;
	}

	.login-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #007aff;
		color: #fff;
		font-size: 32rpx;
		border-radius: 10rpx;
		margin-top: 30rpx;
	}

	.login-btn:disabled {
		background-color: #99bfff;
	}

	.other-actions {
		display: flex;
		justify-content: space-between;
		margin-top: 40rpx;
		padding: 0 20rpx;
	}

	.link {
		font-size: 26rpx;
		color: #007aff;
	}
</style>